<?php
/*include("general_include.php");
$action_arr=array("list_order","list_search","list_paginate");
if(in_array($action,$action_arr))
{
	$IsProcess = 'Y';
	$smarty->assign('IsProcess',"Y");
	
}
if($IsProcess <> 'Y')
	 include "top.php";
$page_name='findstudent.php';
$action=$_REQUEST['action'];
*/


$page_name=basename($_SERVER['SCRIPT_FILENAME']);
$IsPreserved	= 'Y';
$IsProcess		= $_REQUEST['IsProcess'];
$action  =  $_REQUEST['action'];
$action_arr = array("getSrchResult");
if(in_array($action,$action_arr))
{
	$IsProcess = 'Y';
}
include "general_include.php";

$sort_by = $_REQUEST['sorting_by'];
$list_for = $_REQUEST['list_for'];
$availability_country = $_REQUEST['availability_country'];
$game_id = $_REQUEST['game_id'];
$availability = $_REQUEST['availability'];
$language_id = $_REQUEST['language_id'];
$price = $_REQUEST['price'];
if($price == '')
  $price = '0;100';
if($game_id == '')
   $game_id =array();
if($availability == '')
   $availability = array();   
if($language_id == '')
  $language_id = array();


if(count($game_id)>0&& is_array($game_id)&& !empty($game_id))
{
	$game_ids = implode(',',$game_id);
	$smarty->assign('games',$game_ids);
	$sqlPart .= " AND ug.game_id IN($game_ids) AND ug.user_id=u.user_id";
}

if(count($language_id)>0&& is_array($language_id)&& !empty($language_id))
{
	$language_ids = implode(',',$language_id);
	$smarty->assign('languages',$language_ids);
	foreach($language_id as $val)
	$sqlPart .= " AND FIND_IN_SET($val,u.language_ids)";
}
if(!empty($price))
{
   $price_range = explode(";",$price);
   $min_price = $price_range[0];
   $max_price = $price_range[1];
   if($min_price == '0' && $max_price == '100')
    $sqlPart .='';
   else if($min_price == '0' && $max_price <100)
    $sqlPart .=" AND u.rate <='$max_price'";
   else if($min_price >'0' && $max_price == '100')
    $sqlPart .=" AND u.rate >='$min_price'";
   else 
     $sqlPart .=" AND u.rate BETWEEN '$min_price' AND '$max_price'";		  
}   
if(count($availability)>0&& is_array($availability)&& !empty($availability))
{
	$availabilitys = implode(',',$availability);
	$smarty->assign('availabilitys',$availabilitys);
	 foreach($availability as $val)
	 $sqlPart .= " AND FIND_IN_SET('$val',u.availability_type)";
}
if(!empty($availability_country))
{
   $sqlPart .= " AND u.availability_country ='$availability_country'";
}

if($IsProcess <> 'Y')
{
	// include "top.php";
}
$record_per_page = !empty($record_per_page)?$record_per_page:15;
$paginationObj = new PaginationClassFrontAjax($record_per_page,3,1,'Next','Previous');


function findlanguage($language_id)
{
     if($language_id=='')
			return false;
			
		$SelectTypeSql = "SELECT language_name FROM ".TABLEPREFIX."_language WHERE language_id=$language_id";
		$SelectType = mysql_fetch_array(mysql_query($SelectTypeSql));
		return $SelectType[0];
}		


function findvalue($table,$fld_name,$fld_value,$find_value)
{
     if($fld_value=='')
			return false;
			
		$SelectTypeSql = "SELECT ".$find_value." FROM ".$table." WHERE $fld_name=".$fld_value;
		$SelectType = mysql_fetch_array(mysql_query($SelectTypeSql));
		return $SelectType[0];
}
if($IsProcess != 'Y')
 {
     unset($_SESSION['sql']);
     unset($_SESSION['sort']);
	if($list_for == 'sorting')
	{
	  $sortBy = explode('-',$sort_by);
	  
	  if($sortBy[0] == 'rate')
	  {
		if($sortBy[1]=="DESC")
		 $sqlPart.=" ORDER BY u.rate DESC";
		else 
		 $sqlPart.=" ORDER BY u.rate ASC";
	  } 
	}
	else 
       $sqlPart.=" ORDER BY u.name ASC";	
   
   $smarty->assign('sort_by',$sort_by);
   $_SESSION['sql'] = $sqlPart;
   $_SESSION['sort'] = $sort_by;
  
   $SelectstudentSQL="SELECT DISTINCT u.*,ur.user_id FROM ".TABLEPREFIX."_user u ,".TABLEPREFIX."_user_type_user_relation ur,".TABLEPREFIX."_user_game ug  WHERE 
   ur.user_type_id=2 AND   ur.user_id=u.user_id AND  u.is_active='Y'".$sqlPart;
  }
 else 
 {
   $studentSQl = $_SESSION['sql'];
   $smarty->assign('sort_by',$_SESSION['sort']);
   $SelectstudentSQL="SELECT DISTINCT u.*,ur.user_id FROM ".TABLEPREFIX."_user u ,".TABLEPREFIX."_user_type_user_relation ur,".TABLEPREFIX."_user_game ug  
   WHERE ur.user_type_id=2 AND   ur.user_id=u.user_id AND  u.is_active='Y'".$studentSQl;
 }
 
 $pagination_arr = $paginationObj->PaginationFrontAjaxALL('PaginationSearch',$SelectstudentSQL,$page_name.'?'.$catLink);
 
 $StudentArr=$UserManagerObjAjax->GetRecords("All",$pagination_arr[0]);
	//echo $pagination_arr[0];
 $NumStudent=count($StudentArr);
	
for($i=0;$i<$NumStudent;$i++)
{
	$StudentArr[$i]['name']=show_to_control($StudentArr[$i]['name']);
	$StudentArr[$i]['about']=show_to_control($StudentArr[$i]['user_about']);
	$slanguage = explode(",",$StudentArr[$i]['language_ids']);
	$language='';
	for($l=0;$l<count($slanguage);$l++)
	{
	   if($slanguage[$l]<>'')
	    $language .=findlanguage($slanguage[$l]).' , ';
	}
	$StudentArr[$i]['language']=substr($language,0,-2);   
	//$game='';
	$StudentgameSql = "SELECT * FROM ".TABLEPREFIX."_user_game where user_id='".$StudentArr[$i]['user_id']."' and is_active='Y'";
	$StudentgameArr[$i] = $UserManagerObjAjax->GetRecords("All",$StudentgameSql);
    $Numstudentgame=count($StudentgameArr[$i]);
	for($g=0;$g<$NumStudentGame;$g++)
	{
	   $StudentGameArr[$i][$g]['game'] = findvalue(TABLEPREFIX."_game","game_id",$StudentGameArr[$i][$g]['game_id'],"game_name");
	}  
	 
	//$game = findvalue(TABLEPREFIX."_game","game_id",$StudentArr[$i]['game_id'],"game_name");
	//$StudentArr[$i]['game'] = substr($game,0,-1);  
	$avail_type = explode(',',$StudentArr[$i]['availability_type']);
	if($avail_type[1]=='')
	{
	  if($avail_type[0]=='O')
	   $StudentArr[$i]['availability_type'] = 'Online'; 
	  if($avail_type[0]=='L')
	  {
	   $StudentArr[$i]['availability_type'] = 'Local meet-up';
	   $StudentArr[$i]['avail_local'] = 'Y'; 
	  } 
	}
	else
	{
	 $StudentArr[$i]['availability_type'] = 'Online , Local meet-up';
	 $StudentArr[$i]['avail_local'] = 'Y';
	} 
	
	$StudentArr[$i]['availability_country'] = findvalue(TABLEPREFIX."_country","country_id",$StudentArr[$i]['availability_country'],"country_name");
	
	
}


$studentSql="SELECT u.*,ur.* FROM ".TABLEPREFIX."_user u ,".TABLEPREFIX."_user_type_user_relation ur  WHERE ur.user_type_id=2 and ur.user_id=u.user_id 
 and u.is_active='Y'".$searchSql;


$smarty->assign('IsProcess',$IsProcess);
$smarty->assign('SearchTxt',$SearchTxt);
$smarty->assign('catLink',$catLink);
$smarty->assign('page_name',$page_name);
$smarty->assign('record_per_page',$record_per_page);
$smarty->assign('price',$price);
$smarty->assign('language_id',$language_id);
$smarty->assign('game_id',$game_id);
$smarty->assign('availability',$availability);
$smarty->assign('availability_country',$availability_country);
$smarty->assign('StudentArr',$StudentArr);
$smarty->assign('pagination_arr',$pagination_arr);	
$smarty->assign('NumStudent',$NumStudent);
$smarty->assign('NumStudentGame',$NumStudentGame);
$smarty->assign('StudentGameArr',$StudentGameArr);
$smarty->assign('totRec',$totRec);
//$smarty->register_modifier("in_array","in_array");

if($IsProcess <>'Y')
  include "student_leftpanel.php";
$smarty->display('findstudent.tpl');

//if($IsProcess <> 'Y')  include "footer.php";
?>